<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['putStorage:rhmTh:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="type"
          @click="handleUpdate"
          v-hasPermi="['putStorage:rhmTh:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="type"
          @click="handleDelete"
          v-hasPermi="['putStorage:rhmTh:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-circle-check"
          size="mini"
          :disabled="type || tjFlag"
          @click="handleSubmit"
          v-hasPermi="['putStorage:rhmTh:submitRhd']"
        >
          提交
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['putStorage:rhmTh:export']"
        >
          导出
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExportMx"
          v-hasPermi="['putStorage:rhmTh:exportMx']"
        >
          导出明细
        </el-button>
      </el-col>
      <Tag type="dot" size="default" color="#0fac09">提交</Tag>
      <Tag type="dot" size="default" color="#bb0331">通过</Tag>
      <Tag type="dot" size="default" color="#0323af">检验</Tag>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      :height="tableH"
      v-loading="loading"
      :data="rhmThList"
      @row-click="clickRhm"
      :cell-style="iCellStyle"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column
        label="退货单号"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="rhmDjbh"
      />
      <el-table-column
        label="退货日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rhmThrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rhmThrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="客户代码"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="rhmKhdm"
      >
        <template #default="scope">
          <dict-tag :options="allCustomerOptions" :value="scope.row.rhmKhdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="收货单号"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="rhmShdh"
      />
      <el-table-column
        label="仓库代码"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="rhmCkdm"
      >
        <template #default="scope">
          <dict-tag :options="allStorageOptions" :value="scope.row.rhmCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="rhmDesc"
      />
      <el-table-column
        label="拒收备注"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="rhmNgdesc"
      />
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="rhmJlry"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rhmJlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rhmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="检验人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="rhmJyry"
      />
      <el-table-column
        label="检验日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rhmJyrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rhmJyrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="过账人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="rhmGzry"
      />
      <el-table-column
        label="过账日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rhmGzrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rhmGzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAddMx"
            :disabled="scope.row.rhmTjflag != 0"
            v-hasPermi="['putStorage:rhmTh:addRhd']"
          >
            添加
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-table border stripe :height="tableHChild" :data="rhdDetList">
      <el-table-column
        label="#"
        type="index"
        width="40"
        align="center"
        fixed="left"
      />
      <el-table-column label="物料代码" align="center" prop="rhdWldm" />
      <el-table-column label="规格描述" align="center" prop="rhdPmgg" />
      <el-table-column
        label="单位"
        :show-overflow-tooltip="true"
        width="60"
        align="center"
        prop="rhdDw"
      >
        <template #default="scope">
          <dict-tag :options="unitFlagOptions" :value="scope.row.rhdDw" />
        </template>
      </el-table-column>
      <el-table-column label="存放位置" align="center" prop="rhdCwdm">
        <template #default="scope">
          <dict-tag :options="allCwOptions" :value="scope.row.rhdCwdm" />
        </template>
      </el-table-column>
      <el-table-column label="申请数量" align="center" prop="rhdSqsl" />
      <el-table-column label="允收数量" align="center" prop="rhdOksl" />
      <el-table-column label="拒收数量" align="center" prop="rhdNgsl" />
      <el-table-column label="入库数量" align="center" prop="rhdRksl" />
      <el-table-column label="检验结果" align="center" prop="rhdJyjg" />
      <el-table-column label="结果描述" align="center" prop="rhdJyms" />
      <el-table-column
        label="检验人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="rhdJyry"
      />
      <el-table-column
        label="检验日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rhdJyrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rwdJyrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="入库人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="rhdRkry"
      />
      <el-table-column
        label="入库日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="rhdRkrq"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.rwdRkrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        width="60"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleResult(scope.row)"
            v-hasPermi="['putStorage:rhmTh:deleteRhd']"
            :disabled="type"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="450px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="88px"
      >
        <el-col :span="24">
          <el-form-item label="退货单号" prop="rhmDjbh">
            <el-input
              v-model="queryParams.rhmDjbh"
              placeholder="请输入退货单号"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="建立日期">
            <el-date-picker
              v-model="daterangeRhmJlrq"
              size="small"
              :clearable="false"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="入库仓库" prop="rhmCkdm">
            <el-select
              v-model="queryParams.rhmCkdm"
              style="width: 240px"
              clearable
              filterable
              placeholder="请选择仓库"
            >
              <el-option
                v-for="item in allStorageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态">
            <el-radio-group v-model="queryParams.rhmZtbz">
              <el-radio
                v-for="dict in RhmZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改产品退货入库开单作业对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="退货单号" prop="rhmDjbh">
          <el-input v-model="form.rhmDjbh" disabled />
        </el-form-item>
        <el-form-item label="退货日期" prop="rhmThrq">
          <el-date-picker
            clearable
            size="small"
            v-model="form.rhmThrq"
            type="date"
            :clearable="false"
            value-format="yyyy-MM-dd"
            placeholder="选择退货日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="客户代码" prop="rhmKhdm">
          <el-select v-model="form.rhmKhdm" placeholder="请选择客户">
            <el-option
              v-for="dict in customerOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="退货仓库" prop="rhmCkdm">
          <el-select v-model="form.rhmCkdm" placeholder="请选择入库仓库">
            <el-option
              v-for="item in storageOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收货单号" prop="rhmShdh">
          <el-input v-model="form.rhmShdh" placeholder="请输入收货单号" />
        </el-form-item>
        <el-form-item label="备注" prop="rhmDesc">
          <el-input v-model="form.rhmDesc" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <!--  添加明细对话框    -->
    <el-dialog
      v-model="openChildren"
      :fullscreen="true"
      :close-on-click-modal="false"
      append-to-body
    >
      <!-- 子项搜索条件框-->
      <el-form :model="queryChildParams" :inline="true">
        <el-row>
          <el-col :span="6">
            <el-form-item label="物料代码" prop="rhdWldm">
              <el-input
                v-model="queryChildParams.rhdWldm"
                placeholder="请输入物料代码"
                clearable
                size="small"
                @keyup.enter.native="searchMx"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规则描述" prop="rhdPmgg">
              <el-input
                v-model="queryChildParams.rhdPmgg"
                placeholder="请输入规则描述"
                clearable
                size="small"
                @keyup.enter.native="searchMx"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="display: flex; justify-content: flex-end">
              <el-button type="primary" @click="searchMx">查询</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!--子项数据框-->
      <el-form>
        <el-table
          highlight-current-row
          v-loading="loading"
          :data="childAddList"
          border
          stripe
          :height="tableHChild"
          @click
          ref="rhmThChildTable"
          @scrollBottom="load2"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            type="index"
            min-width="2"
            align="center"
          />
          <el-table-column
            label="物料代码"
            :show-overflow-tooltip="true"
            min-width="12"
            align="center"
            prop="rhdWldm"
          />
          <el-table-column
            label="品名规格"
            :show-overflow-tooltip="true"
            min-width="30"
            prop="rhdPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            min-width="10"
            align="center"
            prop="rhdDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.rhdDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="存放位置"
            min-width="10"
            align="center"
            prop="rhdCwdm"
          >
            <template #default="scope">
              <dict-tag :options="allCwOptions" :value="scope.row.rhdCwdm" />
            </template>
          </el-table-column>

          <el-table-column
            label="最小包装"
            :show-overflow-tooltip="true"
            min-width="10"
            align="center"
            prop="rhdMinPackage"
          />

          <el-table-column
            label="已申请数量"
            :show-overflow-tooltip="true"
            min-width="10"
            align="center"
            prop="rhdSqsl"
          />

          <el-table-column
            label="需申请数"
            :show-overflow-tooltip="true"
            min-width="10"
            prop="rhdNeedSqsl"
            align="center"
          >
            <template #default="scope">
              <el-input
                class="inputName"
                type="number"
                @input.native="numberChage(scope.row)"
                v-model="scope.row.rhdNeedSqsl"
              ></el-input>
            </template>
          </el-table-column>
        </el-table>

        <el-table border stripe :height="tableHChild" :data="rhdDetList">
          <el-table-column label="#" width="40" align="center" fixed="left" />
          <el-table-column label="物料代码" align="center" prop="rhdWldm" />
          <el-table-column label="规格描述" align="center" prop="rhdPmgg" />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="60"
            align="center"
            prop="rhdDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.rhdDw" />
            </template>
          </el-table-column>
          <el-table-column label="存放位置" align="center" prop="rhdCwdm">
            <template #default="scope">
              <dict-tag :options="allCwOptions" :value="scope.row.rhdCwdm" />
            </template>
          </el-table-column>
          <el-table-column label="申请数量" align="center" prop="rhdSqsl" />
          <el-table-column label="允收数量" align="center" prop="rhdOksl" />
          <el-table-column label="拒收数量" align="center" prop="rhdNgsl" />
          <el-table-column label="入库数量" align="center" prop="rhdRksl" />
          <el-table-column label="检验结果" align="center" prop="rhdJyjg" />
          <el-table-column label="结果描述" align="center" prop="rhdJyms" />
          <el-table-column
            label="检验人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="rhdJyry"
          />
          <el-table-column
            label="检验日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="rhdJyrq"
            width="180"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.rwdJyrq, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="入库人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="rhdRkry"
          />
          <el-table-column
            label="入库日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="rhdRkrq"
            width="180"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.rwdRkrq, "{y}-{m}-{d}") }}</span>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="60"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleResult(scope.row)"
                v-hasPermi="['putStorage:rhmTh:deleteRhd']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <el-form-item
          style="margin-top: 25px; display: flex; justify-content: flex-end"
        >
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="handAddChildren">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRhmTh,
  getRhmTh,
  delRhmTh,
  addRhmTh,
  updateRhmTh,
  exportRhmTh,
  getMx,
  addMx,
  getOrderByDjbh,
  deleteMxByCondition,
  submitMx,
  exportRhmThMx,
} from "@/api/putStorage/rhmTh";
import { getAllCustomer, getAllCustomerTwo } from "@/api/project/customer";
import { getAll, getstorageType } from "@/api/repertory/Storage";

export default {
  name: "RhmTh",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      //有效客户
      customerOptions: [],
      //所有客户
      allCustomerOptions: [],
      //有效仓库
      storageOptions: [],
      //所有仓库
      allStorageOptions: [],

      //所有仓位
      allCwOptions: [],
      //单位
      unitFlagOptions: [],
      //添加View
      openChildren: false,
      //明细表格数据
      childAddList: [],

      type: true,

      //底部明细数据
      rhdDetList: [],
      tjFlag: true,
      tableH: (document.body.clientHeight - 130) / 2,
      tableHChild: (document.body.clientHeight - 280) / 2,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      //明细总条数
      mxTotal: 0,
      // 产品退货入库开单作业表格数据
      rhmThList: [],
      //保存当前行数据
      rhmData: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 建立日期时间范围
      daterangeRhmJlrq: [
        this.$moment().startOf("month").toDate(),
        this.$moment().add(100, "year").endOf("month").toDate(),
      ],

      //保存的明细数据
      rhdDataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        rhmDjbh: null,
        rhmZtbz: "1",
        rhmJlrq: null,
      },
      //添加View查询参数
      queryChildParams: {
        pageNum: 1,
        pageSize: 15,
        rhmCkdm: null,
        rhmDjbh: null,
        rhdWldm: null,
        rhdPmgg: null,
        rhdKh: null,
      },
      //请求状态
      RhmZtbzOptions: [
        {
          dictLabel: "未检验",
          dictValue: "1",
        },
        {
          dictLabel: "已检验",
          dictValue: "2",
        },
        {
          dictLabel: "全部",

          dictValue: "",
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        rhmThrq: [
          { required: true, message: "退货日期不能为空", trigger: "blur" },
        ],
        rhmKhdm: [
          { required: true, message: "客户代码不能为空", trigger: "blur" },
        ],
        rhmCkdm: [
          { required: true, message: "仓库代码不能为空", trigger: "blur" },
        ],
        rhmTjflag: [
          { required: true, message: "仓库代码不能为空", trigger: "blur" },
        ],
        rhmZtbz: [
          {
            required: true,
            message: "状态标志(1:新增,2:审核,6:过账)不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    getAllCustomer().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusCode;
        i.listClass = "default";
      }

      this.customerOptions = response.data;
    });

    getAllCustomerTwo().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusCode;
        i.listClass = "default";
      }

      this.allCustomerOptions = response.data;
    });

    getstorageType("0").then((response) => {
      this.storageOptions = response.data;

      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });

    getAll().then((response) => {
      this.allStorageOptions = response.data;

      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });

    getAll().then((response) => {
      this.allCwOptions = response.data;

      for (let item2 of this.allCwOptions) {
        item2.dictLabel = item2.storageDefaultstorage;

        item2.listClass = "default";
        item2.dictValue = item2.storageId + "";
      }
    });

    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
  },
  methods: {
    /** 颜色事件 */
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //提交
      if (row.rhmZtbz == "1" && row.rhmTjflag == "1") {
        return "color:#0fac09";
      }
      //通过
      else if (row.rhmZtbz == "2" && row.rhmTjflag == "1") {
        return "color:#0323af";
      }
      //检验
      else if (row.rhmZtbz == "6" && row.rhmTjflag == "1") {
        return "color:#bb0331";
      }
    },
    /** 查询产品退货入库开单作业列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRhmJlrq && "" != this.daterangeRhmJlrq) {
        this.queryParams.params["beginRhmJlrq"] = this.moment(
          this.daterangeRhmJlrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endRhmJlrq"] = this.moment(
          this.daterangeRhmJlrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listRhmTh(this.queryParams).then((response) => {
        this.rhmThList = [];
        this.rhdDetList = [];
        this.rhmThList = this.rhmThList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickRhm(row);
        } else if (this.rhmThList != null && this.rhmThList.length > 0) {
          this.clickRhm(this.rhmThList[0]);
        }
      });
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 15;
        this.queryParams.pageNum++;
        listRhmTh(this.queryParams).then((response) => {
          this.rhmThList = this.rhmThList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
          if (row != null) {
            this.clickRhm(row);
          } else if (this.rhmThList != null && this.rhmThList.length > 0) {
            this.clickRhm(this.rhmThList[0]);
          }
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.openChildren = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rhmId: null,
        rhmDjbh: null,
        rhmThrq: new Date(),
        rhmKhdm: null,
        rhmShdh: null,
        rhmCkdm: null,
        rhmDesc: null,
        rhmTjflag: null,
        rhmNgdesc: null,
        rhmZtbz: "1",
        rhmJlry: null,
        rhmJlrq: null,
        rhmJyry: null,
        rhmJyrq: null,
        rhmGzry: null,
        rhmGzrq: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.rhmThList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRhmJlrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品退货入库开单作业";
    },
    handleAddMx() {
      //初始化查询条件
      this.queryChildParams.rhmCkdm = this.rhmData.rhmCkdm;
      this.queryChildParams.rhmDjbh = this.rhmData.rhmDjbh;
      this.queryChildParams.rhdKh = this.rhmData.rhmKhdm;

      //调用查询全部明细方法
      this.searchMx();

      this.openChildren = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rhmId = row.rhmId || this.rhmData.rhmId;
      getRhmTh(rhmId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品退货入库开单作业";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询产品退货入库开单作业";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.rhmId != null) {
            updateRhmTh(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRhmTh(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rhmIds = row.rhmId || this.rhmData.rhmId;
      this.$confirm(
        '是否确认删除产品退货入库开单作业编号为"' + rhmIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delRhmTh(rhmIds);
        })
        .then(() => {
          this.queryParams.pageNum = 1;
          this.rhmThList = [];
          this.total = 0;
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    //明细查询初始化
    searchMx() {
      this.realSearchMx();
      this.getMxByDjbh(this.rhmData.rhmDjbh);
    },
    isEmpty(rhdDataList) {
      for (let i of rhdDataList) {
        if (i.rhdNeedSqsl == null || i.rhdNeedSqsl == "" || i.rhdNeedSqsl < 0) {
          return true;
        }
      }
      return false;
    },
    //明细确认按钮
    handAddChildren() {
      const datas = this.childAddList.filter((a) => {
        return !!a.rhdNeedSqsl;
      });
      if (datas.length < 1) {
        this.msgError("请输入合法数量~");
        return;
      }
      addMx(datas).then((response) => {
        this.msgSuccess("新增成功");
        //添加明细数据成功后重新请求
        this.searchMx();
      });
    },
    //查询要添加的明细
    realSearchMx() {
      this.queryChildParams.pageNum = 1;
      getMx(this.queryChildParams).then((response) => {
        // this.childAddList = response.rows;
        this.childAddList = [];
        this.childAddList = this.childAddList.concat(response.rows);
        this.mxTotal = response.total;
      });
    },
    //分页
    load2() {
      if (
        this.queryChildParams.pageNum <
        Math.ceil(this.mxTotal / this.queryChildParams.pageSize)
      ) {
        this.queryChildParams.pageSize = 15;
        this.queryChildParams.pageNum++;
        getMx(this.queryChildParams).then((response) => {
          // this.childAddList = response.rows;
          this.childAddList = this.childAddList.concat(response.rows);
          this.mxTotal = response.total;
        });
      }
      return 0;
    },
    //单击查询明细
    clickRhm(row) {
      this.rhmData = row;
      this.type = !(row != null && row.rhmZtbz == "1" && row.rhmTjflag == "0");
      if (row.rhmDjbh) {
        this.getMxByDjbh(row.rhmDjbh);
      }
    },

    //根据单号查询明细
    getMxByDjbh(order) {
      getOrderByDjbh(order).then((response) => {
        this.rhdDetList = response.data;
        if (this.rhdDetList.length === 0) {
          this.tjFlag = true;
        } else {
          this.tjFlag = false;
        }
      });
    },
    //删除
    handleResult(row) {
      if (row) {
        deleteMxByCondition(row.rhdDjbh, row.rhdWldm).then((response) => {
          this.msgSuccess("删除成功");

          //添加明细数据成功后重新请求
          this.searchMx();
        });
      }
    },

    handleSubmit() {
      this.$confirm("确认提交该单据吗?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return submitMx(this.rhmData.rhmDjbh);
        })
        .then((response) => {
          this.msgSuccess("提交成功");
          this.queryParams.pageNum = 1;
          this.rhmThList = [];
          this.getList(this.rhmData);
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有产品退货入库开单作业数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportRhmTh(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },

    handleExportMx() {
      const queryParams = this.rhmData.rhmDjbh;
      this.$confirm("是否确认导出所有产品退货入库开单作业数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportRhmThMx(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },

    numberChage(row) {
      if (row.rhdNeedSqsl != null && row.rhdNeedSqsl != "") {
        if (row.rhdNeedSqsl <= 0) {
          this.msgError("请输入大于0的数字");
          row.rhdNeedSqsl = 1;
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
